// mircoApp/communityMarket/index/index.js
var location = require("../../../mybehaviors/location/location");
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');

var app = getApp();

Page({
  behaviors: [location,skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    defaultImg: '',
    showReceivePop: false,
    awardData: {},
    imgPath: app.globalData.imgPath,
    info: {},
    pageType: 'qr', //qrcode为加群页面，award为领取奖励页面
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {mode,store_id,is_alone,chg} = options;
    if(mode) this.setData({
      pageType: mode,
      chg: chg||0
    });
    console.log(this.data.pageType)
    if(store_id||mode == 'qrStore'){
      this._getLocation((mapdata, e) => {
        let params = {
          lng: e.lng,
          lat: e.lat,
        };
        if(store_id) params.store_id = store_id;
        if(is_alone) params.is_alone = is_alone;
        this.getData(params);
      });
    } else {
      this.getData();
    }
  },
  //获取数据
  getData(_params = {}) {
    let _url = 'plugin.community-marketing.frontend.controllers.index.index';
    if(this.data.pageType == 'award') {
      _url = 'plugin.community-marketing.frontend.controllers.index.rewardPage';
      _params.store_id = this.options.store_id;
    }
    _params.mode = this.options.mode;
    let urlStr = app.getNetAddresss(_url);
    app._postNetWork({
      url: urlStr,
      data: _params,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info: res.data,
            defaultImg: app.globalData.imgPath + 'plugin/community-marketing/top-default.png'
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = "";
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== "") _Param += key + "=" + otherParam[key] + "&";
    });
    if (url === "") return;
      wx.redirectTo({
        url: url + "?" + _Param.slice(0, -1),
        fail: ()=> {
          wx.reLaunch({
            url: url + "?" + _Param.slice(0, -1)
          });
        }
      });
  },
  onReceive() {
    let _params = {
      // test: 1
    };
    if(this.options.store_id) _params.store_id = this.options.store_id;
    let urlStr = app.getNetAddresss('plugin.community-marketing.frontend.controllers.index.receiveNow');
    app._postNetWork({
      url: urlStr,
      data: _params,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            awardData: res.data,
            showReceivePop: true
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  onReceiveAward(){
    if(this.data.awardData.status == 1) {
      return wx.navigateTo({
        url: "/mircoApp/communityMarket/index/index?status=0&mode=qr&is_alone=1&store_id=" + this.options.store_id||0,
      });
    }
    let _URL = this.data.info.button_link_mini || '/packageG/member_v2/member_v2';
    try {
      wx.redirectTo({
        url: _URL
      });
    } catch (error) {
      wx.switchTab({
        url: _URL
      });
    }
  },
  showReceivePop(e){
    let status = e.currentTarget.dataset.status;
    this.setData({showReceivePop: status})
  },

  completemessage(e){
    console.log('completemessage',e)
  },
  startmessage(e){
    console.log('startmessage',e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let {mode,store_id,is_alone,chg} = this.options;
    let _params = `mode=${mode}`;
    if(store_id) _params = _params + `&store_id=${store_id}`;
    if(is_alone) _params = _params + `&is_alone=${is_alone}`;
    if(chg) _params = _params + `&chg=${chg}`;
    return {
      title: '入群新人礼',
      path: `/mircoApp/communityMarket/index/index?${_params}`,
    };
  }
})